package com.sorted;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.sorted.ad.SortMaxReward;
import com.sorted.utils.SortToastUtil;

public class SortSDKManager implements Application.ActivityLifecycleCallbacks {

    public static SortSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final SortSDKManager GAME_APPLICATION = new SortSDKManager();
    }

    public void init(Activity activity) {
        Context mApplication = activity.getApplicationContext();
        registerActivityLifecycleCallbacks(mApplication);
        SortBusiness.init(mApplication);

        initAd(mApplication);

        SortToastUtil.init(activity);
    }

    private void initAd(Context mApplication) {
        AppLovinPrivacySettings.setHasUserConsent(true);
        AppLovinPrivacySettings.setDoNotSell(false);

        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        SortSDK.decrypt(SortConstant.MAX_KEY))
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                SortMaxReward.getInstance().loadRewardAd(mApplication);
            }
        });
    }

    private void registerActivityLifecycleCallbacks(Context mApplication) {
        ((Application) mApplication).registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        SortBusiness.onActivityCreate(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }





}
